﻿Imports System
Imports MindFusion.Diagramming
Imports DevExpress.XtraGrid.Columns

Public Class UcnGrid
    'represents PopupContainerControl for setting properties of row of table

#Region "Attributes"
#Region "Private attributes"
    Private MoItem As DiagramItem = Nothing  'actual item
    Private MoOldCellValue As Object = Nothing 'old cell value after value was changed to new value
#End Region
#End Region
#Region "Events"
    Public Event MMValueChanged(ByVal Column As GridColumn, ByVal NewValue As Object, ByVal OldValue As Object) 'event is raised when value of cell was successful changed
    Public Event MMPositionOfNavigatorChanged(ByVal Position As Integer) 'event is raised after position of navigator is changed
    Public Event MMAddItem(ByRef Item As DiagramItem, ByVal NewRowPosition As Integer, ByVal NewDataNavigatorPosition As Integer) 'event is raised when user clicked on add button (before adding new item)
    Public Event MMRemoveItem(ByRef Item As DiagramItem, ByVal ColumnName As String, ByVal NewDataNavigatorPosition As Integer) 'event is raised when user clicked on remove button (before adding new item)
    Public Event MMMoveUp(ByRef Item As DiagramItem, ByVal ColumnName As String) 'event is raised when user clicked on up button (before moving item)
    Public Event MMMoveDown(ByRef Item As DiagramItem, ByVal ColumnName As String) 'event is raised when user clicked on down button (before moving item)
#End Region
#Region "Constructors"
    Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        tlTableLayoutPanel.BackColor = Me.DataNavigator.BackColor
        MoItem = Nothing
    End Sub
#End Region
#Region "Properties"
#Region "Public attributes"
    Public ReadOnly Property Position() As Integer
        Get
            If BindingSource.Count = 0 Then
                Return -1
            Else
                Return DataNavigator.Position
            End If
        End Get
    End Property
#End Region
#End Region
#Region "Events handler"
    'adding and removing row
    Private Sub DataNavigator_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.NavigatorButtonClickEventArgs) Handles DataNavigator.ButtonClick
        Select Case e.Button.ButtonType
            Case DevExpress.XtraEditors.NavigatorButtonType.Custom
                Select Case e.Button.Tag
                    Case "ADD_BUTTON"
                        e.Handled = True
                        RaiseEvent MMAddItem(MoItem, DataNavigator.Position + 2, CalculateNewPositionOfDataNavigator(DataNavigator.Position, 1))
                    Case "REMOVE_BUTTON"
                        e.Handled = True
                        RaiseEvent MMRemoveItem(MoItem, CType(BindingSource.Item(DataNavigator.Position), DataRow).Item("ColumnName"), CalculateNewPositionOfDataNavigator(DataNavigator.Position, -1))
                    Case "UP_BUTTON"
                        e.Handled = True
                        RaiseEvent MMMoveUp(MoItem, CType(BindingSource.Item(DataNavigator.Position), DataRow).Item("ColumnName"))
                    Case "DOWN_BUTTON"
                        e.Handled = True
                        RaiseEvent MMMoveDown(MoItem, CType(BindingSource.Item(DataNavigator.Position), DataRow).Item("ColumnName"))
                End Select
            Case DevExpress.XtraEditors.NavigatorButtonType.First
                RaiseEvent MMPositionOfNavigatorChanged(0)
            Case DevExpress.XtraEditors.NavigatorButtonType.Prev
                RaiseEvent MMPositionOfNavigatorChanged(DataNavigator.Position - 1)
            Case DevExpress.XtraEditors.NavigatorButtonType.Next
                RaiseEvent MMPositionOfNavigatorChanged(DataNavigator.Position + 1)
            Case DevExpress.XtraEditors.NavigatorButtonType.Last
                RaiseEvent MMPositionOfNavigatorChanged(BindingSource.Count - 1)
        End Select
    End Sub
    'saving old (original) value of cell for later use
    Private Sub CardView_ShownEditor(ByVal sender As Object, ByVal e As System.EventArgs) Handles CardView.ShownEditor
        MoOldCellValue = GridView.ActiveEditor.EditValue()
    End Sub
#End Region
#Region "Subs"
    'initialization of controls
    Public Sub Init(ByVal IoItem As DiagramItem, ByRef OoDataRow() As DataRow, Optional ByVal IiSelectedPosition As Integer = 0, Optional ByVal IsDataNavigatorIsVisible As Boolean = False)
        DataNavigator.Visible = IsDataNavigatorIsVisible

        'update data
        CardView.BeginUpdate()
        BindingSource.DataSource = OoDataRow
        CardView.PopulateColumns(OoDataRow)
        CardView.CardWidth = 100

        MoItem = IoItem

        If MoItem IsNot Nothing Then
            Select Case MoItem.GetType.Name
                Case "ClsMMContainerNode"
                    DataNavigator.Position = IiSelectedPosition
                Case "ClsMMTableNode"
                    DataNavigator.Position = IiSelectedPosition
                Case "ClsMMLinkNode"
                Case Else
                    Debug.Print("CHYBA!!! - UcnGrid - Init")
            End Select
        End If

        If OoDataRow IsNot Nothing Then
            If OoDataRow.Length > 0 Then SetView(OoDataRow(0).Table.TableName)
        End If

        RaiseEvent MMPositionOfNavigatorChanged(DataNavigator.Position)

        CardView.EndUpdate()
        SetEnable()
    End Sub
    'sets names and visibility of columns
    Private Sub SetView(ByVal IsTableName As String)
        Try
            Dim LoCollectionOfVisibleColumns As Collection = New Collection
            Select Case IsTableName
                Case "_ModulStructure"
                    CardView.Columns("UnitName").Caption = "Nazov unitu"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("UnitName"), "UnitName")
                Case "_Tables"
                    CardView.Columns("AppTableName").Caption = "Nazov tabulky"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("AppTableName"), "AppTableName")
                    CardView.Columns("_a_IsSupport").Caption = "Support tabulka"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("_a_IsSupport"), "_a_IsSupport")
                Case "_a_Columns"
                    CardView.Columns("ResValue").Caption = "Hodnota v res"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("ResValue"), "ResValue")
                    CardView.Columns("ColumnName").Caption = "Datovy nazov stlpca"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("ColumnName"), "ColumnName")
                    CardView.Columns("_a_ColumnType").Caption = "Datovy typ"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("_a_ColumnType"), "_a_ColumnType")
                    CardView.Columns("_a_ColumnType").ColumnEdit = RepositoryItemComboBoxDataType
                    CardView.Columns("_a_ComponentType").Caption = "Typ komponentu"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("_a_ComponentType"), "_a_ComponentType")
                    CardView.Columns("_a_ComponentType").ColumnEdit = RepositoryItemComboBoxComponentType
                    CardView.Columns("JoinModuleID").Caption = "Napojeny modul"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("JoinModuleID"), "JoinModuleID")
                    CardView.Columns("DefaultValue").Caption = "Default"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("DefaultValue"), "DefaultValue")
                    CardView.Columns("StrongBind").Caption = "Silna vazba"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("StrongBind"), "StrongBind")
                    CardView.Columns("_a_Access").Caption = "Pristup"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("_a_Access"), "_a_Access")
                    CardView.Columns("_a_Access").ColumnEdit = RepositoryItemComboBoxAccess
                    CardView.Columns("Decimals").Caption = "Des. miesta"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("Decimals"), "Decimals")
                    CardView.Columns("Description").Caption = "Popis"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("Description"), "Description")
                    CardView.Columns("ResShortValue").Caption = "Hodnota skratky v res"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("ResShortValue"), "ResShortValue")
                    CardView.Columns("Validate").Caption = "Kontrolovat"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("Validate"), "Validate")
                    CardView.Columns("Validate").ColumnEdit = RepositoryItemComboValidateType
                    CardView.Columns("MaxValue").Caption = "Max"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("MaxValue"), "MaxValue")
                    CardView.Columns("MinValue").Caption = "Min"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("MinValue"), "MinValue")
                    CardView.Columns("ValidateText").Caption = "Text pri chybe"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("ValidateText"), "ValidateText")
                    CardView.Columns("Format").Caption = "Maska"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("Format"), "Format")

                    'TEMP! TEMP! TEMP!
                    'LoCollectionOfVisibleColumns.Add(CardView.Columns("Position"), "Position-DOCASNE")
                Case "_a_Version"
                    CardView.Columns("_a_Created").Caption = "Verzia vytvorena"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("_a_Created"), "_a_Created")
                    CardView.Columns("_a_ModulName").Caption = "Nazov"
                    LoCollectionOfVisibleColumns.Add(CardView.Columns("_a_ModulName"), "_a_ModulName")
                Case Else
                    Debug.Print("CHYBA!!! - UcnGrid - SetView -  Select Case IsTableName")
            End Select

            For Each LoColumn As GridColumn In CardView.Columns
                LoColumn.Visible = False
            Next

            For Each LoColumn As GridColumn In LoCollectionOfVisibleColumns
                LoColumn.Visible = True
                'If Not IsInCollection(LoCollectionOfVisibleColumns, LoColumn.FieldName) Then LoColumn.Visible = False
            Next
        Catch ex As Exception
            Debug.Print("CHYBA!!! - UcnGrid - SetView")
        End Try
    End Sub
    'clear datasource
    Public Sub Clear()
        BindingSource.DataSource = Nothing
    End Sub
    'set enable settings for controls
    Private Sub SetEnable()
        DataNavigator.Buttons.CustomButtons(0).Enabled = True
        DataNavigator.Buttons.CustomButtons(1).Enabled = Not DataNavigator.Position = -1
        If BindingSource.DataSource IsNot Nothing Then
            Dim LiNumberOfItems As Integer = CType(BindingSource.DataSource, DataRow()).Length
            DataNavigator.Buttons.CustomButtons(2).Enabled = DataNavigator.Position > 0
            DataNavigator.Buttons.CustomButtons(3).Enabled = (DataNavigator.Position + 1 < LiNumberOfItems) And (LiNumberOfItems > 1)
        Else
            DataNavigator.Buttons.CustomButtons(2).Enabled = False
            DataNavigator.Buttons.CustomButtons(3).Enabled = False
        End If
    End Sub
#End Region
#Region "Functions"
    'calculation of new position in DataNavigator after adding or removing row from grid
    Function CalculateNewPositionOfDataNavigator(ByVal IiOldPosition As Integer, ByVal IiCountChange As Integer) As Integer
        If IiCountChange = 1 Then Return IiOldPosition + 1 'if adding new row

        'if removing row
        If IiOldPosition = BindingSource.Count - 1 Then 'last row
            Return BindingSource.Count - 1 - 1
        Else
            Return IiOldPosition
        End If
    End Function
#End Region

   


    'TODO
    Private Sub CardView_ValidatingEditor(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles CardView.ValidatingEditor
        If e.Value.GetType.Name = "String" Then
            Select Case CardView.FocusedColumn.Caption
                Case "Datovy typ"
                    Select Case e.Value
                        Case "Boolean"
                            e.Value = CInt(kkXmodColumnTypes.kkBoolean)
                            e.Valid = True
                        Case "Cele cislo"
                            e.Value = CInt(kkXmodColumnTypes.kkInt32)
                            e.Valid = True
                        Case "Desatinne cislo"
                            e.Value = CInt(kkXmodColumnTypes.kkDecimal)
                            e.Valid = True
                        Case "Text"
                            e.Value = CInt(kkXmodColumnTypes.kkText)
                            e.Valid = True
                        Case "Datum"
                            e.Value = CInt(kkXmodColumnTypes.kkDatum)
                            e.Valid = True
                        Case "Cas"
                            e.Value = CInt(kkXmodColumnTypes.kkCas)
                            e.Valid = True
                        Case "Datum a Cas"
                            e.Value = CInt(kkXmodColumnTypes.kkDatetime)
                            e.Valid = True
                        Case Else
                            e.Value = GsStringDefaultValue
                            e.Valid = False
                    End Select
                Case "Typ komponentu"
                    'TODO: citat z datasetu komponentov
                    Select Case e.Value
                        Case "Ziadny"
                            e.Value = CInt(kkComponentTypes.kkNone)
                            e.Valid = True
                        Case "xComboSuper"
                            e.Value = kkComponentTypes.kkxComboSuper
                            e.Valid = True
                        Case "xButton"
                            e.Value = kkComponentTypes.kkxButton
                            e.Valid = True
                        Case "xTextBox"
                            e.Value = kkComponentTypes.kkxTextBox
                            e.Valid = True
                        Case "xGrid"
                            e.Value = kkComponentTypes.kkxGrid
                            e.Valid = True
                        Case Else
                            e.Value = GsStringDefaultValue
                            e.Valid = False
                    End Select

                Case ("Pristup")
                    Select Case e.Value
                        Case "R/W"
                            e.Value = CInt(kkXmodAccessTypes.kkRW)
                            e.Valid = True
                        Case "R"
                            e.Value = CInt(kkXmodAccessTypes.kkR)
                            e.Valid = True
                        Case Else
                            e.Value = GsStringDefaultValue
                            e.Valid = False
                    End Select
                Case "Kontrolovat"
                    Select Case e.Value
                        Case "-"
                            e.Value = CInt(kkValidationTypes.kkNone)
                            e.Valid = True
                        Case "Zakladna"
                            e.Value = CInt(kkValidationTypes.kkZakladna)
                            e.Valid = True
                        Case "Unikatnost"
                            e.Value = CInt(kkValidationTypes.kkUnikatnost)
                            e.Valid = True
                        Case "Zakladna + Unikatnost"
                            e.Value = CInt(kkValidationTypes.kkZakladnaAUnikatnost)
                            e.Valid = True
                    End Select
            End Select
        End If
    End Sub

    Private Sub RepositoryItemComboBoxDataType_CustomDisplayText(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs) Handles RepositoryItemComboBoxDataType.CustomDisplayText
        If e.Value.GetType.ToString = "System.Int32" Then
            Select Case CInt(e.Value)
                Case kkXmodColumnTypes.kkBoolean
                    e.DisplayText = "Boolean"
                Case kkXmodColumnTypes.kkInt32
                    e.DisplayText = "Cele cislo"
                Case kkXmodColumnTypes.kkDecimal
                    e.DisplayText = "Desatinne cislo"
                Case kkXmodColumnTypes.kkText
                    e.DisplayText = "Text"
                Case kkXmodColumnTypes.kkDatum
                    e.DisplayText = "Datum"
                Case kkXmodColumnTypes.kkCas
                    e.DisplayText = "Cas"
                Case kkXmodColumnTypes.kkDatetime
                    e.DisplayText = "Datum a Cas"
                Case Else
                    e.DisplayText = GsStringDefaultValue
            End Select
        End If
    End Sub

    Private Sub RepositoryItemComboBoxComponentType_CustomDisplayText(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs) Handles RepositoryItemComboBoxComponentType.CustomDisplayText
        If e.Value.GetType.ToString = "System.Int32" Then
            Select Case CInt(e.Value)
                Case kkComponentTypes.kkNone
                    e.DisplayText = "Ziadny"
                Case kkComponentTypes.kkxComboSuper
                    e.DisplayText = "xComboSuper"
                Case kkComponentTypes.kkxButton
                    e.DisplayText = "xButton"
                Case kkComponentTypes.kkxTextBox
                    e.DisplayText = "xTextBox"
                Case kkComponentTypes.kkxGrid
                    e.DisplayText = "xGrid"
                Case Else
                    e.DisplayText = GsStringDefaultValue
            End Select
        End If
    End Sub

    Private Sub RepositoryItemComboBoxAccess_CustomDisplayText(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs) Handles RepositoryItemComboBoxAccess.CustomDisplayText
        If e.Value.GetType.ToString = "System.Int32" Then
            Select Case CInt(e.Value)
                Case kkXmodAccessTypes.kkR
                    e.DisplayText = "R"
                Case kkXmodAccessTypes.kkRW
                    e.DisplayText = "R/W"
                Case Else
                    e.DisplayText = GsStringDefaultValue
            End Select
        End If
    End Sub

    Private Sub RepositoryItemComboValidateType_CustomDisplayText(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs) Handles RepositoryItemComboValidateType.CustomDisplayText
        If e.Value.GetType.ToString = "System.Int32" Then
            Select Case CInt(e.Value)
                Case kkValidationTypes.kkNone
                    e.DisplayText = "-"
                Case kkValidationTypes.kkZakladna
                    e.DisplayText = "Zakladna"
                Case kkValidationTypes.kkUnikatnost
                    e.DisplayText = "Unikatnost"
                Case kkValidationTypes.kkZakladnaAUnikatnost
                    e.DisplayText = "Zakladna + Unikatnost"
            End Select
        End If
    End Sub

    Private Sub CardView_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles CardView.CellValueChanged
        Debug.Print("CardView_CellValueChanged")
        RaiseEvent MMValueChanged(e.Column, e.Value, MoOldCellValue)
    End Sub

    Private Sub DataNavigator_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataNavigator.PositionChanged
        SetEnable()
    End Sub
End Class

'!!!!!!!!!!!!!!!!!!!!!!!!
'Private Sub gcGridControl_ProcessGridKey(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles gcGridControl.ProcessGridKey
